﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace StorageBox
{
    public class MainTabControlReader : ReaderViewBase
    {
        private TabControl _rootElement;
        private ListBox _stateMenuSelectList;

        public MainTabControlReader(MainWindow main) : base(main)
        {
            _rootElement = main.MainTabControl;
            _stateMenuSelectList = main.StateMenuSelectList;
        }

        public override void Show()
        {
            Console.WriteLine("错误输出");
        }

        public override void Hide()
        {
            SetMainTabControlState = 0;
        }

        private MainTabControlState _setMainTabControlState;
        public MainTabControlState SetMainTabControlState
        {
            get { return _setMainTabControlState; }
            set
            {
                SetIndex((int)value);
                _setMainTabControlState = value;
            }
        }


        private void SetIndex(int index)
        {
            if ((int)_setMainTabControlState == index)
            {
                return;
            }
            if (index != 0)
                _rootElement.Visibility = System.Windows.Visibility.Visible;

            switch ((MainTabControlState)index)
            {
                case MainTabControlState.None:
                    _rootElement.Visibility = System.Windows.Visibility.Hidden;
                    break;
                case MainTabControlState.AddApply:
                    //加载开始菜单
                    List<Shortcut> applyList;
                    _curData = new AddRangApplyData(_stateMenuSelectList);
                    break;
                default:
                    break;
            }

            _rootElement.SelectedIndex = index;

            //没有确定按钮,只写到了显示出来
            //选择逻辑也没有写,后台数据没有变
        }

        /// <summary>
        /// 当前页面数据[这个设计真是sb到爆了]
        /// </summary>
        private TabControlData _curData;

        public T GetCurrData<T>() where T : TabControlData
        {
            return (T)_curData;
        }
    }




    public class TabControlData
    { }

    public class AddRangApplyData : TabControlData
    {
        private Dictionary<int, Shortcut> _shortcut = new();
        public AddRangApplyData(ListBox listBox)
        {
            listBox.Items.Clear();
            _shortcut = new();

            GetStateMenuData();

            //渲染出来
            foreach (var item in _shortcut)
            {
                listBox.Items.Add(new
                {
                    Name = item.Value.Name,
                    ImagePath = getBitMapSourceFromBitmap(item.Value.IconPath),
                    CheckBoxUid = item.Key
                });
            }
        }

        public void SetChecked(int index, bool data)
        {
            if (_shortcut.ContainsKey(index))
            {
                _shortcut[index].IsJudge = data;
            }
        }

        public List<Shortcut> GetCheckedData()
        {
            return _shortcut.GetValues().Where(u => u.IsJudge).ToList();
        }


        /// <summary>
        /// Bitmap转ImageSource
        /// </summary>
        private System.Windows.Media.Imaging.BitmapSource getBitMapSourceFromBitmap(Bitmap bitmap)
        {
            IntPtr intPtrl = bitmap.GetHbitmap();
            BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(intPtrl,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            DeleteObject(intPtrl);
            return bitmapSource;
        }

        [System.Runtime.InteropServices.DllImport("gdi32")]
        static extern int DeleteObject(IntPtr o);

        private void GetStateMenuData()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + @"\Programs";
            int index = 0;

            List<DirectoryInfo> list = new List<DirectoryInfo>();
            GetDirectoryList(path, list);
            foreach (var item in list)
            {
                foreach (var flie in Directory.GetFiles(item.FullName))
                {
                    if (flie.EndsWith(".lnk"))
                    {
                        _shortcut.Add(index++, new()
                        {
                            IsJudge = false,
                            Path = flie,
                            IconPath = Icon.ExtractAssociatedIcon(flie).ToBitmap()
                        });
                    }
                }
            }
        }

        private void GetDirectoryList(string filePath, List<DirectoryInfo> directoryList)
        {
            //目录不存在
            if (!Directory.Exists(filePath)) return;
            DirectoryInfo thisDirectory = new DirectoryInfo(filePath);
            directoryList.Add(thisDirectory);
            foreach (DirectoryInfo directory in thisDirectory.GetDirectories())
            {
                GetDirectoryList(directory.FullName, directoryList);
            }
        }

    }




    public class Shortcut
    {
        public string Name
        {
            get
            {
                var index = Path.LastIndexOf('\\');
                return Path.Substring(Path.LastIndexOf('\\') + 1, (Path.Length - index) - (Path.Length + 1 - Path.LastIndexOf('.')));
            }
        }
        public bool IsJudge { get; set; }
        public string Path { get; set; }
        public System.Drawing.Bitmap IconPath { get; set; }
    }




    public enum MainTabControlState
    {
        None = 0,//关闭
        AddApply = 1,//批量添加应用
    }
}
